﻿namespace ConsoleApp2
{
    //2 异常筛选器
    /*
       Syetem.IndexOutOfRangeException	数组越界
       System.ArithmaticException	    算术操作异常的基类
       System.DivideByZeroException	    除零错误
       System.OutOfMemoryException	    用 new 分配内存失败
       System.StackOverflowException	递归过多、过深
       System.NullReferenceException	对象为空
     */

    internal class Program
    {
       static void Main(string[] args)
    {
        try
        {
            int result = Divide(10, 0);
            Console.WriteLine("Result: " + result);
        }
        catch (DivideByZeroException ex) when (IsExpected(ex))
        {
            Console.WriteLine("Expected error: " + ex.Message);
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine("Unexpected error: " + ex.Message);
        }
    }

    static bool IsExpected(DivideByZeroException ex)
    {
        return ex.Message == "Expected error";
    }

    static int Divide(int x, int y)
    {
        if (y == 0)
        {
            throw new DivideByZeroException("Expected error");
        }

        return x / y;
    }
    }
}